//
//  TravelsFirst_FirstTableViewCell.m
//  艾听歌
//
//  Created by dllo on 16/5/28.
//  Copyright © 2016年 dllo. All rights reserved.
//
#import "TravelsFirst_FirstTableViewCell.h"

#define CellWidth self.contentView.frame.size.width
#define CellHeight self.contentView.frame.size.height

#define FrontPhotoImageW self.frontPhotoImage.frame.size.width
#define FrontPhotoImageH self.frontPhotoImage.frame.size.height

#define LeftSpace 10
#define TopSpace 20

@implementation TravelsFirst_FirstTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createSubViews];
    }
    
    return self;
}


- (void)createSubViews{
    
    self.frontPhotoImage = [[UIImageView alloc]init];
    self.frontPhotoImage.backgroundColor = [UIColor magentaColor];
    [self.contentView addSubview:self.frontPhotoImage];
    self.frontPhotoImage.userInteractionEnabled = YES;
    

    
    self.nameLabel = [[UILabel alloc]init];

    [self.contentView addSubview:self.nameLabel];
    self.nameLabel.userInteractionEnabled = YES;
    self.nameLabel.textColor = [UIColor whiteColor];
    self.nameLabel.font = [UIFont systemFontOfSize:25];
    self.nameLabel.numberOfLines = 0;
    
    
    self.timeLabel = [[UILabel alloc]init];
//    self.timeLabel.backgroundColor = [UIColor blueColor];
    [self.contentView addSubview:self.timeLabel];
    self.timeLabel.userInteractionEnabled = YES;
    self.timeLabel.textColor = [UIColor whiteColor];
    
    
    self.userImage = [[UIImageView alloc]init];
    self.userImage.backgroundColor = [UIColor yellowColor];
    [self.contentView addSubview:self.userImage];
    self.userImage.userInteractionEnabled = YES;
    
    
    UITapGestureRecognizer *travelsTap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(travelsTapAction)];
    UITapGestureRecognizer *travelsTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(travelsTapAction)];
    UITapGestureRecognizer *travelsTap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(travelsTapAction)];
    
    UITapGestureRecognizer *userImageTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(userImageTapAction)];
    
    
    [self.frontPhotoImage addGestureRecognizer:travelsTap1];
    [self.nameLabel addGestureRecognizer:travelsTap2];
    [self.timeLabel addGestureRecognizer:travelsTap3];
    
    [self.userImage addGestureRecognizer:userImageTap];
    
    
    
}


- (void)layoutSubviews{
    
    
    self.frontPhotoImage.frame = CGRectMake(LeftSpace, TopSpace / 4, CellWidth - LeftSpace * 2, CellHeight - TopSpace / 2);
    
    //调整照片渐变效果
    CGFloat a= 1;
    for (double i= 0; i< self.frontPhotoImage.frame.size.height / 2; i++) {
        a = ( i / self.frontPhotoImage.frame.size.height / 2);
        UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, self.frontPhotoImage.frame.size.height / 2 - i, self.frontPhotoImage.frame.size.width, 1)];
        view.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha: a / 4];
        [self.frontPhotoImage addSubview:view];
    }
    self.frontPhotoImage.alpha = 0.8;
    self.frontPhotoImage.layer.cornerRadius = 8;
    self.frontPhotoImage.clipsToBounds = YES;
    
    
    //lable的自适应高度
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:25], NSFontAttributeName,  nil];
    
    CGRect rect = [_cellModel.name boundingRectWithSize:CGSizeMake(FrontPhotoImageW - LeftSpace * 2, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    self.nameLabel.frame = CGRectMake(LeftSpace * 2, TopSpace + TopSpace / 4, FrontPhotoImageW - LeftSpace * 2, rect.size.height);
    
    self.timeLabel.frame = CGRectMake(LeftSpace * 2,TopSpace / 4 + TopSpace + self.nameLabel.frame.size.height, self.nameLabel.frame.size.width, (FrontPhotoImageH - TopSpace * 2) / 5 / 2);
    
    
    self.userImage.frame = CGRectMake(LeftSpace * 2, CellHeight - TopSpace - (FrontPhotoImageH - TopSpace * 2) / 5 - TopSpace / 4, (FrontPhotoImageH - TopSpace * 2) / 5, (FrontPhotoImageH - TopSpace * 2) / 5);
    self.userImage.layer.masksToBounds = YES;
    self.userImage.layer.cornerRadius = self.userImage.frame.size.height / 2;
}


- (void)setCellModel:(ModuleFirst_TravelsTitleModel *)cellModel{
    
    _cellModel = cellModel;
    
    [self.frontPhotoImage sd_setImageWithURL:[NSURL URLWithString:_cellModel.front_cover_photo_url] placeholderImage:[UIImage imageNamed:@"holderr"]];
    
    
    self.nameLabel.text = _cellModel.name;
    
    NSString *start_date = _cellModel.start_date;
    NSString *days = _cellModel.days.stringValue;
    NSString *photos_count = _cellModel.photos_count.stringValue;
    NSString *time = [NSString stringWithFormat:@"%@/%@天/%@图片",start_date, days, photos_count];
    self.timeLabel.text = time;
    
    
    [self.userImage sd_setImageWithURL:[NSURL URLWithString:_cellModel.userModel.image] placeholderImage:[UIImage imageNamed:@"holderr"]];
    
    
}





//点击跳到游记详情页面
- (void)travelsTapAction{
    
    self.travelsDetailsBlock(self.cellModel);
    
}

//点击跳到作者主页
- (void)userImageTapAction{
    
    self.userHomepageBlock(_cellModel.userModel);
    
}



@end
